﻿//用于商家翻译页面标识翻译添加、修改、删除、查询

var $dataAddTemplate = null;
var $dataListTemplate = null;
var $tbodyAdd = null;
var $tbodyList = null;
var ajaxUrl = "Handler.ashx";

//DOM樹加載完成
$(function () {
    $dataAddTemplate = $("#dataAddTemplate");
    $dataListTemplate = $("#dataListTemplate");
    $tbodyAdd = $("#tbodyAdd");
    $tbodyList = $("#tbodyList");

    getTranslatePageDDL();//获取翻译页面下拉框
    addEmptyRow(5);//添加空行
    getFlagTranslateList();//获取标识翻译结果
});

var $ddlPage = null;
var $ddlAddPage = null;
var pageDDLOptionsHtml = null;

//獲取翻譯頁面下拉框
function getTranslatePageDDL()
{
    $ddlPage = $("#ddlPage");
    $ddlAddPage = $("#ddlAddPage");

    var data = {
        "Operate": "GetTranslatePageDDL",
        "SortField": "PageId",
        "IsDesc": false
    };
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            pageDDLOptionsHtml = $.templates($("#pageDDLTemplate").text()).render(ajaxMsg.data);
            $ddlPage.append(pageDDLOptionsHtml);//搜索
            $ddlAddPage.append(pageDDLOptionsHtml);//添加         
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var flagIndex = 0;//標識索引

//添加空行
//rowCount 行數
function addEmptyRow(rowCount) {
    var datas = [];
    for (var i = 1; i <= rowCount; i++) {
        var data = {};
        data.FlagId = ++flagIndex;
        data.PageId = 0;
        data.Flag = "";
        data.ResultHK = "";
        data.ResultCN = "";
        data.ResultEN = "";
        datas.push(data);
    }

    var html = $.templates($dataAddTemplate.text()).render(datas);
    $tbodyAdd.append(html);
}

//检查翻译标识是否存在
function isExistsTranslateFlag(txtFlag)
{
    var flag = $(txtFlag).val();
    if (flag == "")
    {
        return;
    }

    var data = {
        "Operate": "IsExistsTranslateFlag",
        "Flag": flag
    };

    msgBox.showMsgWait(txt_IdentityCheck); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            msgBox.showMsgOk(txt_IdentityBeUsed); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
            txtFlag.focus();
        }
    }, "json");
}

//添加所有標識翻譯
function addAllFlagTranslate()
{
    var strPageId = $ddlAddPage.val();
    if (isNaN(strPageId)) {
        alert(txt_TranslationPageError);
        return;
    }

    var pageId = parseInt(strPageId);
    if (pageId < 0) {
        alert(txt_SelectranslationPage);
        $ddlAddPage.focus();
        return;
    }

    var $form = $("#formAdd");
    var data = $form.serialize();
    data += "&Operate=AddAllFlagTranslate&FlagCount=" + flagIndex + "&PageId=" + pageId;

    msgBox.showMsgWait(txt_Save); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            var $tr = null;
            var strI = null;
            $tbodyAdd.children().each(function (i) {
                strI = ++i;
                $tr = $(this);
                $ddlAddPage.val("0");
                $("#txtFlag_" + strI, $tr).val("");
                $("#txtResultHK_" + strI, $tr).val("");
                $("#txtResultCN_" + strI, $tr).val("");
                $("#txtResultEN_" + strI, $tr).val("");
            });

            getFlagTranslateList();
            msgBox.showMsgOk(txt_Success); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var isFirstLoad=true;//是否是首次加載數據
var sortField = null;
var isDesc = null;

//獲取排序字段和方法
function getSortInfo()
{
    sortField = "PageId";
    isDesc = false;
    return;
}

//获取標識翻譯列表
function getFlagTranslateList() {
    var strPageId = $("#ddlPage").val();
    if (isNaN(strPageId)) {
        alert(txt_TranslationPageError);
        return;
    }

    var keyword=$("#txtKeyword").val();
    getSortInfo();//獲取排序信息

    var data = {
        "Operate": "GetFlagTranslateList",
        "PageId": strPageId,
        "SortField": sortField,
        "IsDesc": isDesc,
        "Keyword": keyword
    };

    msgBox.showMsgWait(txt_Loading); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            if (ajaxMsg.data.length > 0) {//有数据
                var html = $.templates($dataListTemplate.text()).render(ajaxMsg.data);
                $tbodyList.html(html);

                $tbodyList.children().each(function (i) {
                    var $tr = $(this);
                    var $ddlEditPage = null;
                    var strFlagId = $tr.attr("id").replace("tr_", "");
                    $ddlEditPage = $("#ddlEditPage_" + strFlagId, $tr);
                    $ddlEditPage.append(pageDDLOptionsHtml);
                    $ddlEditPage.prepend($ddlPage.children().eq(0).clone());//添加"所有頁面"到最前面
                    $ddlEditPage.val($ddlEditPage.attr("pageid"));
                });
            }
            else {
                $tbodyList.children().remove();
                $tbodyList.html("<tr><td colspan=" + $tbodyList.attr("colspan") + ">" + txt_NoData + "</td></tr>");
            }

            msgBox.hidBox(); //隐藏消息面板
            return;//以下为分页代码，暂时没用

            var colspan=$tbody.prev().children().eq(0).children().length;
            if ($tbodyList.children().length > 0) {//如果已有数据，先移去
                $tbodyList.children().remove();
            }

            if(ajaxMsg.data.length>0)
            {
                $dataListTemplate.tmpl(ajaxMsg).appendTo($tbody); //添加数据

                $('#pagination-demo').twbsPagination({//分頁
                    totalPages: ajaxMsg.totalPages,
                    visiblePages: 10,
                    onPageClick: function (event, page) {
                        if(!isFirstLoad)
                        {                                    
                            getFiledList(page);
                        }
                        isFirstLoad=false;
                    },
                    first: txt_FirstPage,
                    prev: txt_PreviousPage,
                    next: txt_NextPage,
                    last: txt_LastPage
                });
            }
            else{
                $("<tr><td colspan='" + colspan + "'>" + txt_NoData + "</td></tr>").appendTo($tbody);
            }
            msgBox.hidBox(); //隐藏消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var flag = null;
var resultHK = null;
var resultCN = null;
var resultEN = null;

//修改標識翻譯
//btnSave 當前點擊的保存按鈕
function editFlagTranslate(btnEdit)
{
    var strFlagId = $(btnEdit).attr("id").replace("btnEdit_", "");
    if (isNaN(strFlagId)) {
        alert(txt_Error);
        return;
    }

    var flagId = parseInt(strFlagId);
    if (flagId <= 0) {
        alert(txt_Error);
        return;
    }

    var $tr = $("#tr_" + flagId, $tbodyList);

    var $ddlEditPage = $("#ddlEditPage_" + flagId, $tr);
    var strPageId = $ddlEditPage.val();
    if (isNaN(strPageId)) {
        alert(txt_TranslationPageError);
        return;
    }

    var pageId = parseInt(strPageId);
    if (pageId < 0) {
        alert(txt_SelectranslationPage);
        $ddlEditPage.focus();
        return;
    }

    var $txtFlag = $("#txtFlag_" + flagId, $tr);
    var $txtResultHK = $("#txtResultHK_" + flagId, $tr);
    var $txtResultCN = $("#txtResultCN_" + flagId, $tr);
    var $txtResultEN = $("#txtResultEN_" + flagId, $tr);

    if ($txtFlag.length > 0 && $txtResultHK.length > 0 && $txtResultCN.length > 0 && $txtResultEN.length > 0)
    {
        if ($txtFlag.attr("id") != "txtFlag_" + strFlagId
            || $txtResultHK.attr("id").replace("txtResultHK_", "") != strFlagId
            || $txtResultCN.attr("id").replace("txtResultCN_", "") != strFlagId
            || $txtResultEN.attr("id").replace("txtResultEN_", "") != strFlagId)
        {
            alert(txt_Error);
            return;
        }

        flag = $txtFlag.val();
        resultHK = $txtResultHK.val();
        resultCN = $txtResultCN.val();
        resultEN = $txtResultEN.val();
        var data = {
            "Operate": "EditFlagTranslate",
            "PageId": pageId,
            "FlagId": flagId,
            "Flag": flag,
            "ResultHK": resultHK,
            "ResultCN": resultCN,
            "ResultEN": resultEN,
        };

        msgBox.showMsgWait(txt_TryingToModify); //显示消息面板
        $.post(ajaxUrl, data, function (ajaxMsg) {
            if (ajaxMsg.status == 1) {
                $ddlEditPage.attr("pageid", pageId);//更新頁面Id
                msgBox.showMsgOk(txt_Success); //顯示消息面板
            }
            else {
                msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
                $txtFlag.focus();
            }
        }, "json");

        return;
    }
    else
    {
        alert(txt_TranslationPageError);
        return;
    }
}

//刪除標識翻譯
//btnDelete 當前點擊的刪除按鈕
function deleteFlagTranslate(btnDelete)
{
    if (confirm(txt_SureDelete) == false) {
        return;
    }

    var strFlagId = $(btnDelete).attr("id").replace("btnDelete_", "");
    if (isNaN(strFlagId)) {
        alert(txt_Error);
        return;
    }

    var flagId = parseInt(strFlagId);
    if (flagId <= 0) {
        alert(txt_Error);
        return;
    }

    var $tr = $("#tr_" + flagId, $tbodyList);
    var data = {
        "Operate": "DeleteFlagTranslate",
        "FlagId": flagId
    };

    msgBox.showMsgWait(txt_TryingToDelete); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            $tr.remove();
            msgBox.showMsgOk(txt_Success); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");

    return;
}